<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class daily_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		

public function get_salereport($date){
	$query = $this->db->get_where('sales', array('date' => $date));
	return $query;

	}


public function get_purchasereport($date){
	$query = $this->db->get_where('purchase', array('date' => $date));
	return $query;

	}

public function get_customer($id_user){
		$query = $this->db->get_where('customer', array('id' => $id_user));
		echo $query->row()->name;

}

public function get_product(){
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
		$this->db->from('product', 'category','company' );
		$this->db->join('category', 'product.id_category = category.id' );
		$this->db->join('company', 'product.id_company = company.id');
		//$this->db->where(array('product.code' => $code));
		$q = 	$query = $this->db->get();
		$product = $q->result();
		$new_product=array();
		foreach($product as $data){
			//$data['product'] = $data['code'].'/'.$data['category_name'].'/'.$data['company_name'];	
			$new_product[]=$data;
			
		}
		///print_r($new_product);exit;
		return  $new_product; 
	
	
}

public function get_customers(){
		$this->db->select('customer.*');
	$this->db->from('customer');
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data1[] = $row;
			}
				
			return $data1;
		}
	
	
}
public function get_suppliers(){
	$this->db->select('supplier.*');
	$this->db->from('supplier');
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data2[] = $row;
			}
				
			return $data2;
		}
	
}

public function get_saleproduct($date,$id_product){
	$query = $this->db->get_where('sales', array('date' => $date));
}
public function get_purchaseProduct(){
	
		$query = $this->db->get_where('sales', array('date' => $date));
}

public function getall_sales($id_customer, $start_date, $end_date){
	if($start_date == $end_date){
		if($id_customer=='all'){
				$this->db->select('*');
				$this->db->from('sales');
				$where = "date = '$start_date'";
				$this->db->where($where);
				$all_sales=$this->db->get()->result();
				return $all_sales;
				//print_r($all_sales);exit;
		
		}else{
				$this->db->select('*');
				$this->db->from('sales');
				$where = "date = '$start_date' AND id_customer = $id_customer";
				//echo $where;exit;
				$this->db->where($where);
				$all_sales=$this->db->get()->result();
				return $all_sales;
		}
	}else{
		if($id_customer=='all'){
				$this->db->select('*');
				$this->db->from('sales');
				$where = "date BETWEEN  '$start_date' AND '$end_date' ORDER BY date DESC ";
				$this->db->where($where);
				$all_sales=$this->db->get()->result();
				return $all_sales;
		
		}else{
				$this->db->select('*');
				$this->db->from('sales');
				$where = "id_customer = $id_customer AND date BETWEEN  '$start_date' AND '$end_date' ORDER BY date DESC";
				$this->db->where($where);
				$all_sales=$this->db->get()->result();
				return $all_sales;
		}
				
	}

}
public function getall_purchase($id_supplier,$start_date, $end_date){ 
	if($start_date == $end_date){
		if($id_supplier=='all'){
				$this->db->select('*');
				$this->db->from('purchase');
				$where = "received_date = '$start_date'";
				$this->db->where($where);
				$all_purchases=$this->db->get()->result();
				return $all_purchases;
				//print_r($all_purchases);exit;
		
		}else{
				$this->db->select('*');
				$this->db->from('purchase');
				$where = "received_date = '$start_date' AND id_supplier = $id_supplier";
				//echo $where;exit;
				$this->db->where($where);
				$all_purchases=$this->db->get()->result();
				return $all_purchases;

				
		}
	}else{
		if($id_supplier=='all'){
				$this->db->select('*');
				$this->db->from('purchase');
				$where = "received_date BETWEEN  '$start_date' AND '$end_date' ORDER BY received_date DESC ";
				$this->db->where($where);
				$all_purchases=$this->db->get()->result();
				//echo "without date";
				return $all_purchases;
				//return $all_purchases;
		
		}else{
				$this->db->select('*');
				$this->db->from('purchase');
				$where = "id_supplier = $id_supplier AND received_date BETWEEN  '$start_date' AND '$end_date' ORDER BY received_date DESC";
				$this->db->where($where);
				$all_purchases=$this->db->get()->result();
				return $all_purchases;
		}
				
	}

}
public function get_sale($start_date, $end_date){
	if($start_date == $end_date){
		$sales=$this->db->get_where("sales",array("date"=>$start_date))->result();
		return $sales;
	}else{
		
			$this->db->select('*');
			$this->db->from('sales');
			$where = "date BETWEEN  '$start_date' AND '$end_date' ORDER BY date DESC ";
			$this->db->where($where);
			$sales=$this->db->get()->result();
			return $sales;
	}
	
}

public function get_purchase($start_date, $end_date){ 
		if($start_date == $end_date){
		$purchases=$this->db->get_where("purchase",array("received_date"=>$start_date))->result();
		return $purchases;
		
	}else{
			$this->db->select('*');
			$this->db->from('purchase');
			$where = "received_date BETWEEN  '$start_date' AND '$end_date' ORDER BY received_date DESC ";
			$this->db->where($where);
			$purchases=$this->db->get()->result();
			return $purchases;
	}
	
	
}
public function get_product_sale($id_product, $start_date,$end_date ){
 
	if($start_date == $end_date){
		if($id_product == 'all'){
				$this->db->select('*');
				$this->db->from('sales_product');
				$where = "date = '$start_date'";
				$this->db->where($where);
				$all_sales_products = $this->db->get()->result();
				return $all_sales_products;
				//print_r($all_sales_products);exit;
		
		}else{
		        $this->db->select('sales.invoice_no,sales_product.id_product,sales_product.quantity,sales_product.date,sales_product.total');
				$this->db->from('sales_product','sales');
				$this->db->join('sales','sales.id=sales_product.id_sale');
				$where = "sales_product.id_product = $id_product ";
				$where1="sales_product.date = '$start_date'";
				$this->db->where($where);
				$this->db->where($where1);
				$all_sales_products=$this->db->get()->result();
				return $all_sales_products;
				

				
		}
	}else{
		if($id_product=='all'){
				$this->db->select('sales.invoice_no,sales_product.id_product,sales_product.quantity,sales_product.date,sales_product.total');
				$this->db->from('sales_product','sales');
				$this->db->join('sales','sales.id=sales_product.id_sale');
				$where1="sales_product.date BETWEEN  '$start_date' AND '$end_date'";
				
				$this->db->where($where1);
				$all_sales_products=$this->db->get()->result();
				//echo "without date";
				//print_r($all_sales_products);exit;
				return $all_sales_products;
		
		}else{  
				$this->db->select('sales.invoice_no,sales_product.id_product,sales_product.quantity,sales_product.date,sales_product.total');
				$this->db->from('sales_product','sales');
				$this->db->join('sales','sales.id=sales_product.id_sale');
				$where = "sales_product.id_product = $id_product ";
				$where1="sales_product.date BETWEEN  '$start_date' AND '$end_date'";
				$this->db->where($where);
				$this->db->where($where1);
				$all_sales_products=$this->db->get()->result();
				return $all_sales_products;
				
		}
				
	}
	
	
}
public function get_product_purchase($id_product, $start_date,$end_date){
	if($start_date == $end_date){
		if($id_product == 'all'){
			$this->db->select('purchase_product.id_product,purchase.referance_no,purchase_product.date,purchase_product.quantity,purchase_product.total');
	            $this->db->from('purchase_product','purchase');
				$this->db->join('purchase','purchase_product.id_purchase=purchase.id');
				$where = "date = '$start_date'";
				$this->db->where($where);
				$all_purchase_products = $this->db->get()->result();
				return $all_purchase_products;
				//print_r($all_purchase_products);exit;
		
		}else{
			$this->db->select('purchase_product.id_product,purchase.referance_no,purchase_product.date,purchase_product.quantity,purchase_product.total');
				$this->db->from('purchase_product','purchase');
				$this->db->join('purchase','purchase_product.id_purchase=purchase.id');
				$where = "date = '$start_date' AND id_product = $id_product";
				$this->db->where($where);
				$all_purchase_products = $this->db->get()->result();
				//print_r($all_purchase_products);exit;
				return $all_purchase_products;

		}
	}else{
		if($id_product=='all'){
				$this->db->select('purchase_product.id_product,purchase.referance_no,purchase_product.date,purchase_product.quantity,purchase_product.total');
	            $this->db->from('purchase_product','purchase');
				$this->db->join('purchase','purchase_product.id_purchase=purchase.id');
				$where = "date BETWEEN  '$start_date' AND '$end_date' ORDER BY date DESC ";
				$this->db->where($where);
				$all_purchase_products=$this->db->get()->result();
				//echo "without date";
				return $all_purchase_products;
				//return $all_purchases;
		
		}else{
				$this->db->select('purchase_product.id_product,purchase.referance_no,purchase_product.date,purchase_product.quantity,purchase_product.total');
				$this->db->from('purchase_product','purchase');
				$this->db->join('purchase','purchase_product.id_purchase=purchase.id');
				$where = "id_product = $id_product AND date BETWEEN  '$start_date' AND '$end_date' ORDER BY date DESC";
				$this->db->where($where);
				$all_purchase_products=$this->db->get()->result();
				return $all_purchase_products;
		}
				
	}
	

	
}
public function get_product_stock($id_product, $start_date,$end_date){ 
	if($id_product=='all'){
		$q = $this->db->query("SELECT id_product,date FROM sales_product UNION SELECT id_product,date FROM purchase_product WHERE  id_product = '$id_product' AND date BETWEEN  '$start_date' AND '$end_date'  GROUP BY date");	
	return $q->result();
		
	}else{ 
	//echo "SELECT product_code,date FROM sales_product UNION SELECT product_code,date FROM purchase_product WHERE  product_code = '$id_product' AND date BETWEEN  '$start_date' AND '$end_date'  GROUP BY date";exit;
	$q = $this->db->query("SELECT id_product, date FROM sales_product where id_product='$id_product'AND date BETWEEN  '$start_date' AND '$end_date'  GROUP BY date UNION SELECT id_product,date FROM purchase_product WHERE  id_product = '$id_product' AND date BETWEEN  '$start_date' AND '$end_date'  GROUP BY date");	
	
	return $q->result();
	}
	
}

public function get_purchase_quantity($id_product,$start_date){
	$this->db->select('SUM(quantity) as product_quantity');
	$this->db->from('purchase_product');
	$where1 = "id_product = $id_product AND date  = '$start_date'  GROUP BY date";
	$this->db->where($where1);
	$all_purchase_products = $this->db->get()->row_array();	
	if(count($all_purchase_products)>0){
			return $all_purchase_products['product_quantity'];
	}else{
		return 0;
	}
}

public function get_sales_quantity($id_product,$start_date){ 
	$this->db->select('SUM(quantity) as product_quantity');
	$this->db->from('sales_product');
	$where1 = "id_product = $id_product AND date  = '$start_date'  GROUP BY date";
	$this->db->where($where1);
	$all_sales_products = $this->db->get()->row_array();	
	if(count($all_sales_products)>0){
	
			return $all_sales_products['product_quantity'];
	}else{ 
		return 0;
	}
}

public function  get_product_details($id){
	//$query = $this->db->get_where('product', array('code' => $code));
	//return $query;	
	
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
	$this->db->from('product', 'category','company' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->where(array('product.id' => $id));
	$q = 	$query = $this->db->get();
	$product = $q->row_array();
	
	$product_name = $product['code'].",".$product['category_name'].",".$product['company_name'];
	return  $product_name; 
}

public function get_sales_detail($id)
{
$this->db->select('*');
$this->db->from('sales_product');
$this->db->where(array('id_sale' => $id));
return $this->db->get()->result();
}
public function get_purchase_detail($id)
{
$this->db->select('*');
$this->db->from('purchase_product');
$this->db->where(array('id_purchase' => $id));
return $this->db->get()->result();
}
public function get_product_name($id)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where(array('id' => $id));
 return $this->db->get()->row();
}
public function get_details($id)
{
$this->db->select('*');
$this->db->from('sales');
$this->db->where(array('id' => $id));
return $this->db->get()->row();
}
public function get_purchase_details($id)
{
$this->db->select('*');
$this->db->from('purchase');
$this->db->where(array('id' => $id));
return $this->db->get()->row();
}







}
